package com.burgess.net.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

/**
 * 调用其他的服务
 */
@Configuration
public class AskController {

    //注册本地服务名
    @Value("${spring.application.name}")
    private String name;

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "ask")
    public String ask(){
        // 从client-service服务中请求sayHello服务
        ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("http://EUREKA-CLIENT-SERVICE/hello/{name}", String.class, name);
        String res = responseEntity.getBody();
        return res;
    }

    // 注入一个可以进行负载均衡的RestTemplate用于服务调用
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}